package com.example.start

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.DialogFragment

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [PolicyItemEditFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class PolicyItemEditFragment : DialogFragment() {
    // TODO: Rename and change types of parameters
    var data1 : Int = 0
    lateinit var data2 : String
    lateinit var data3 : String
    private var listener: DialogClickListener? = null
    interface DialogClickListener {
        fun onDialogPositiveClick(dialog: DialogFragment, text1: String, text2: String)
        fun onDialogNegativeClick(dialog: DialogFragment)
    }
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(activity)
        val inflater = requireActivity().layoutInflater
        val view = inflater.inflate(R.layout.fragment_policy_item_edit, null)

        var editText1 = view.findViewById<EditText>(R.id.policy_item_title_edittext)
        var editText2 = view.findViewById<EditText>(R.id.policy_item_url_edittext)

        data1 = arguments?.getInt("data1")!!
        data2 = arguments?.getString("data2").toString()
        data3 = arguments?.getString("data3").toString()

        editText1.setText(data2)
        editText2.setText(data3)
        builder.setView(view)
            .setPositiveButton("OK") { _, _ ->
                listener?.onDialogPositiveClick(
                    this@PolicyItemEditFragment,
                    editText1?.text.toString(),
                    editText2?.text.toString()
                )
            }
            .setNegativeButton("Cancel") { _, _ ->
                listener?.onDialogNegativeClick(this@PolicyItemEditFragment)
            }
            .setTitle("编辑项目")

        return builder.create()
    }
    fun setDialogClickListener(listener: DialogClickListener) {
        this.listener = listener
    }
}